define(function (require, exports, module) {
    exports.ready = function () {
        require.async(['$', 'swiper'], function () {
            $(function(){
                var store_id = $('#store_id').val();
                var showTips = $('#showTips').val();
                var page = 1,flag = true,goods_name;

                getData();
                getList();

                /*搜索*/
                $('.search-btn').click(function(){
                    page = 1;
                    goods_name = $('#search').val();
                    $('#index-item').html('');
                    getList();
                });

                /*滑动加载*/
                $('.container').scroll(function () {
                    // console.log($('.contain').outerHeight());
                    // console.log($('.contain').scrollTop());
                    console.log($('.container').scrollTop() + $('.container').outerHeight()+1);
                    console.log($('.container-child').outerHeight());
                    if (($('.container').scrollTop() + $('.container').outerHeight() + 1) >= ($('.container-child').outerHeight())) {
                        if (flag) {
                            getList();
                        }
                    }
                });

                function getData(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/index',
                        type: 'post',
                        data: {store_id:store_id,showTips:showTips},
                        dataType: 'json',
                        beforeSend: function(){
                            $('.circle-loadding').show();
                        },
                        success: function(data){
                            if(data.status == 'S'){
                                var flash = '',classify_slide = '',classify = '';

                                /*轮播图*/
                                if(!data.flash){

                                }else{
                                    $.each(data.flash,function(i,k){
                                        flash += '<div class="swiper-slide"><img src="'+ k +'"/></div>'
                                    });
                                }

                                $('#banner').html(flash);
                                var mySwiper = new Swiper('.banner', {
                                    pagination : '.banner .swiper-pagination',
                                    centeredSlides: true,
                                    autoplay: 2000,
                                    loop: true
                                });

                                /*分类*/
                                $.each(data.cate,function(i,k){
                                    classify = '';
                                    $.each(k,function(i,k){
                                        classify += '<a href="/restaurant/TrustworthyShop/goodlist/store_id/'+ store_id +'/cate_id/'+ k.cate_id +'" class="classify-blank gc-flex gc-flex-col">' +
                                            '<div class="classify-img"><img src="'+ k.image +'"/></div><p class="gc-ft-deepgray">'+ k.cate_name +'</p></a>';
                                    })
                                    classify_slide += '<div class="swiper-slide gc-ft-center gc-flex">'+ classify +'</div>';
                                });
                                $('#classify').html(classify_slide);
                                var mySwiper1 = new Swiper('.classify', {
                                    pagination : '.classify .swiper-pagination',
                                    slidesPerView: "auto",
                                    centeredSlides: true,
                                });

                                if(data.tips.length > 0){
                                    fm.confirm('<p class="gc-ft-lg confirm_t">公告</p><div class="gc-ft-left gc-ft-indent"><p>'+ data.tips +'</p></div>',function (callback) {
                                        if(callback == 1){
                                            fm.closeLayer();
                                        }
                                    }, 'border-top: 4px solid #00a5f3;width: 64%;',['我知道啦']);
                                }


                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

                function getList(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/goodList',
                        type: 'post',
                        data: {store_id:store_id,p:page,goods_name:goods_name},
                        dataType: 'json',
                        beforeSend: function(){
                            flag = false;
                            $('.no_more').remove();
                            $('.circle-loadding').show();
                        },
                        success: function(data){
                            if(data.status == 'S') {
                                if(data.cartCount > 0){
                                    $('.tips').html(data.cartCount).removeClass('hide');
                                }

                                var goods_list = '',no_more = '';
                                if(data.goods.length > 0){
                                    $.each(data.goods, function (i, k) {
                                        goods_list += '<a href="/restaurant/TrustworthyShop/gooddetail/store_id/' + store_id + '/goods_id/' + k.goods_id + '" class="item gc-flex gc-flex-col gc-flex-tc">' +
                                            '<div class="item-img"><img src="' + k.goods_thumb + '"></div>' +
                                            '<p class="item-name">' + k.goods_name + '</p>' +
                                            '<p class="gc-flex gc-flex-between full">' +
                                            '<span class="gc-ft-red">￥' + k.goods_price + '</span>' +
                                            '<span class="gc-ft-sm gc-ft-gray">已售' + k.sale_num + '</span></p></a>';
                                    });
                                    if(data.goods.length < 6){
                                        flag = false;
                                        no_more += '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">没有更多商品咯~</p>'
                                        $('#index-item').after(no_more);
                                    }else{
                                        page ++;
                                        flag = true;
                                    }
                                    $('#index-item').append(goods_list);
                                }else{
                                    flag = false;
                                    no_more += '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">没有更多商品咯~</p>'
                                    $('#index-item').after(no_more);
                                }
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

            })
        });
    };
});
